<?php

namespace app\cps\controller;

use app\common\Common;
use app\common\Error;
use app\common\JsonRpcClient;
use app\Request;
use think\facade\Db;

class OpenDouyin extends Base
{
    private static JsonRpcClient $jsonRpcClient;

    public function __construct()
    {
        JsonRpcClient::config(config('jsonrpc.address'));
        JsonRpcClient::$timeout = 5;
        self::$jsonRpcClient = JsonRpcClient::instance('admin/v1/AdOceanengine');
    }
    /**
     * 查询巨量详情时间段数据
     *
     * @param int company_id 公司id
     * @param string advertiser_id 账户id
     * @return int      code     状态码
     * @return string   msg      状态信息
     */
    public function getStatisticsChart(Request $request)
    {
    try{
            $rpcData=self::getStatisticsList($request->post('company_id'), $request->post('advertiser_id'));
            if ($rpcData['code'] !== Error::SUCCESS_CODE) {
                //判断是否有linkid
                if(!empty($request->post('link_id'))){
                    $linkFind=Db::name('link')->where('id',$request->post('link_id'))->find();
                    //判断linkid是否有数据
                    if(!empty($linkFind)){
                        $company=Db::name('official_accounts')->where('id',$linkFind['official_account_id'])->find();
                        //判断是否能找到公司id
                        if(!empty($company)){
                            $rpcData=self::getStatisticsList($company['company_id'], $request->post('advertiser_id'));
                            if($rpcData['code'] !== Error::SUCCESS_CODE){
                                return Common::fail($rpcData['msg'], $rpcData['code']);
                            }
                        }else{
                            return Common::fail($rpcData['msg'], $rpcData['code']);
                        }
                    }else{
                        return Common::fail($rpcData['msg'], $rpcData['code']);
                    }
                }else{
                    return Common::fail($rpcData['msg'], $rpcData['code']);
                }
            }
            return Common::success($rpcData['data'], $rpcData['msg']);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

    }
    public static function getStatisticsList($company_id,$advertiser_id){
        $params = [
            'company_id' => $company_id,
            'advertiser_id' => $advertiser_id,
        ];
        $rpcData = self::$jsonRpcClient->getStatisticsChart($params);
        if ($rpcData['code'] !== Error::SUCCESS_CODE) {
            return $rpcData;
        }
        $arr=[];
        $num=0;
        foreach($rpcData['data'] as $key=>$val){
            if(isset($arr[$val['stat_time_hour']])){
                $arr[$val['stat_time_hour']]=round($arr[$val['stat_time_hour']]+(float)$val['metrics']['stat_cost'],2);
            }else{
                $arr[$val['stat_time_hour']]=$val['metrics']['stat_cost'];
            }
            // $num+=$val['metrics']['stat_cost'];
            
        }
        $stats=[];
        foreach($arr as $key=>$val){
          $stats[]=[
                "metrics"=>[
					"stat_cost"=> $val
				],
				"stat_time_hour"=>$key
            ];
        }
        $rpcData['data']=[];
        $rpcData['data']['stats']=$stats;
        return $rpcData;
    }
}
